package cn.qingyun.gis.modules.scenes.conver;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OperatorConverter implements Converter<Integer> {

    private static final Map<Integer, String> TEXT_MAP = new ConcurrentHashMap<>();

    public OperatorConverter() {
        TEXT_MAP.put(0, "移动");
        TEXT_MAP.put(1, "联通");
        TEXT_MAP.put(2, "电信");
        TEXT_MAP.put(3, "广电");
        TEXT_MAP.put(4, "未接入");
    }

    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (value!=null){
            return new WriteCellData<>(TEXT_MAP.getOrDefault(value, ""));
        }
        return new WriteCellData<>("");
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String text = cellData.getStringValue();
        for (Map.Entry<Integer, String> entry : TEXT_MAP.entrySet()) {
            if (entry.getValue().equals(text)) {
                return entry.getKey();
            }
        }
        // 如果找不到匹配的文本，返回0或自定义默认值
        return 0;
    }
}